package com.ruoyi.web.course;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.TVideo;
import com.ruoyi.course.domain.TVideoCategory;
import com.ruoyi.course.mapper.TVideoCategoryMapper;
import com.ruoyi.course.service.ITVideoCategoryService;
import com.ruoyi.course.vo.TVideoCategoryVo;
import com.ruoyi.course.vo.TVideoVo;
import com.ruoyi.course.service.ITVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 视频Controller
 *
 * @author ruoyi
 * @date 2024-12-07
 */
@Api(tags = "视频Controller")
@RestController
@RequestMapping("/course/video")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TVideoController extends BaseController {

    private final ITVideoService tVideoService;
    private final ITVideoCategoryService itVideoCategoryService;

    @ApiOperation("查询视频列表")
    @PreAuthorize("@ss.hasPermi('course:video:list')")
    @GetMapping("/list")
    public TableDataInfo<TVideoVo> list(TVideoVo entity) {
        return tVideoService.queryList(entity);
    }

    @ApiOperation("查询视频所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TVideoVo entity) {
        return AjaxResult.success("查询成功", tVideoService.queryAll(entity));
    }

    @ApiOperation("根据课程id查询视频所有列表")
    @GetMapping("/listAllByCourse/{cId}")
    public AjaxResult listAllByCourse(@PathVariable("cId") Long cId) {
        List<TVideoCategory> categories = itVideoCategoryService.queryByCourseId(cId);
        List<Long> categoryIds = categories.stream()
                .map(TVideoCategory::getId)
                .collect(Collectors.toList());
//        获取视频种类id
        System.err.println(categoryIds);
        List<Long> videoIds = tVideoService.queryVideoIdsByCategoryIds(categoryIds);
        System.err.println(videoIds);
        LambdaQueryWrapper<TVideo> qw = new LambdaQueryWrapper<>();
        qw.in(!StringUtils.isEmpty(videoIds),TVideo::getId, videoIds);
        List<TVideo> list = tVideoService.list(qw);


        return AjaxResult.success("查询成功", list);
    }

    @ApiOperation("导出视频列表")
    @PreAuthorize("@ss.hasPermi('course:video:export')")
    @Log(title = "视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TVideoVo entity) {
        List<TVideoVo> list = tVideoService.queryAll(entity);
        ExcelUtil<TVideoVo> util = new ExcelUtil<>(TVideoVo.class);
        util.exportExcel(response, list, "视频数据");
    }

    @ApiOperation("获取视频详细信息")
    @PreAuthorize("@ss.hasPermi('course:video:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tVideoService.queryById(id));
    }

    @ApiOperation("新增视频")
    @PreAuthorize("@ss.hasPermi('course:video:add')")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TVideoVo entity) {
        return toAjax(tVideoService.save(entity));
    }

    @ApiOperation("修改视频")
    @PreAuthorize("@ss.hasPermi('course:video:edit')")
    @Log(title = "视频", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TVideoVo entity) {
        return toAjax(tVideoService.updateById(entity));
    }

    @ApiOperation("删除视频")
    @PreAuthorize("@ss.hasPermi('course:video:remove')")
    @Log(title = "视频", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tVideoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
